﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace EkspSys
{
    public partial class ArbitraryTraceRouteForm : Form
    {
        private MainWindow main;

        public ArbitraryTraceRouteForm(MainWindow main)
        {
            InitializeComponent();
            this.main = main;
            AcceptButton = startButton;
        }

        private void startButton_Click(object sender, EventArgs e)
        {
            try
            {
                IPHostEntry hostEntry = Dns.GetHostEntry(destinationTextField.Text);
                IPAddress[] ips = hostEntry.AddressList;
                if (ips.Length == 0)
                {
                    MessageBox.Show("Ingen hosts fundet for IP-adressen eller URL'en '" + destinationTextField.Text + "'", "Ukendt destination", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (hostEntry.AddressList.Length > 1)
                {
                    DialogResult res = MessageBox.Show(ips.Length + " hosts fundet for '" + destinationTextField.Text + "'. Trace routes?", "Adskillige hosts fundet", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (res == DialogResult.OK)
                    {
                        foreach (IPAddress ipa in ips)
                        {
                            main.DoTraceRoute(ipa);
                        }
                        Close();
                    }
                }
                else
                {
                    main.DoTraceRoute(Dns.GetHostEntry(destinationTextField.Text).AddressList[0]);
                    Close();
                }
            }
            catch (Exception _)
            {
                MessageBox.Show("'" + destinationTextField.Text + "' kunne ikke kontaktes. Obs.: Destinationen skal være en gyldig og IP-adresse eller URL", "Host ikke tilgængelig", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
